from PyQt5.QtWidgets import QWidget, QVBoxLayout
from PyQt5.QtOpenGL import QGLWidget, QGLFormat
from OpenGL.GL import *
from OpenGL.GLU import *

class RotatingMenuPreview(QGLWidget):
    def __init__(self, parent=None):
        fmt = QGLFormat()
        fmt.setVersion(3, 3)
        fmt.setProfile(QGLFormat.CoreProfile)
        super().__init__(fmt, parent)
        self.rotation = 0.0

    def initializeGL(self):
        glEnable(GL_DEPTH_TEST)
        glClearColor(0.1, 0.1, 0.1, 1.0)

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, w / h, 0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)

        glRotatef(self.rotation, 0, 1, 0)

        # 绘制旋转菜单项（模拟图标）
        for i in range(6):
            angle = i * 60
            glPushMatrix()
            glRotatef(angle, 0, 1, 0)
            glTranslatef(0, 0, -2)
            self._draw_icon(i)
            glPopMatrix()

    def _draw_icon(self, index):
        glBegin(GL_QUADS)
        glColor3f(0.8, 0.8, 1.0)
        glVertex3f(-0.5, -0.5, 0)
        glVertex3f(0.5, -0.5, 0)
        glVertex3f(0.5, 0.5, 0)
        glVertex3f(-0.5, 0.5, 0)
        glEnd()

    def update_rotation(self):
        self.rotation += 1
        if self.rotation >= 360:
            self.rotation = 0
        self.update()